package com.lmxdawn.api.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lmxdawn.api.admin.dao.JadeMapper;
import com.lmxdawn.api.admin.entity.Jade;
import com.lmxdawn.api.admin.entity.Md;
import com.lmxdawn.api.admin.service.IJadeService;
import com.lmxdawn.api.common.api.Constants;
import com.lmxdawn.api.common.exception.NotRealnameException;
import com.lmxdawn.api.common.session.SessionContextHolder;
import com.lmxdawn.api.common.util.IdWorker;
import com.lmxdawn.api.dnf.dto.SaleReq;
import com.lmxdawn.api.dnf.entity.Order;
import com.lmxdawn.api.dnf.entity.OrderItem;
import com.lmxdawn.api.dnf.mapper.OrderItemMapper;
import com.lmxdawn.api.dnf.mapper.OrderMapper;
import lombok.val;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author huangyubin
 * @since 2024-07-04
 */
@Service
public class JadeServiceImpl extends ServiceImpl<JadeMapper, Jade> implements IJadeService {

    @Resource
    private JadeMapper jadeMapper;
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private OrderItemMapper orderItemMapper;
    @Resource
    private IdWorker idWorker;


    @Override
    public void resumeStockNum(Long productId, BigDecimal quantityOrder) {
        jadeMapper.resumeStockNum(productId, quantityOrder);
    }

    @Override
    public void deductStockNum(Long productId, BigDecimal quantity) {
        jadeMapper.discountStockNum(productId, quantity);
    }

    @Override
    public String doSale(SaleReq content, String id) {
        val jade = new Jade();
        if (!SessionContextHolder.get().isRealName()) {
            throw new NotRealnameException();
        }
        if (content.getUnit() == null || content.getUnit().equals("元")){
            // 单次出售多少元 比例= 多少
            jade.setQuantity(content.getQuantity().toString());
        }else{
            jade.setQuantity(BigDecimal.ONE.divide(content.getQuantity(), 2 , RoundingMode.HALF_UP).toString());
        }
        jade.setAmount(content.getAmount().toString());
        jade.setPrice(content.getPrice().toString());
        jade.setGameServer(content.getGameServer());
        jade.setType(content.getType() == 2 ? 2 : 15);
        jade.setCharge("0");
        jade.setSaleUser(id.toString());
        jade.setPlatform(content.getPlatform());
        jade.setValidHour(content.getValidHour());
        jade.setContact(content.getContact());
        jade.setOnlineTime(content.getOnlineTime());
        jade.setUnit(content.getUnit());
        jade.setAttrs(content.getAttrs());
        jade.setImg(content.getImg());
        jade.setLevel(content.getLevel());
        jade.setAttrs(content.getAttrs());
        jade.setAuction(content.getAuction());
        if (content.getId() == null){
            jadeMapper.insert(jade);

//            if (Arrays.asList( 4 , 12 ,14).contains(content.getType())){
//                Order order = new Order();
////                GoldCoin coin = this.lambdaQuery().eq(GoldCoin::getType, content.getType() - 1)
////                        .eq(GoldCoin::getGameServer, content.getGameServer().split("-")[0])
////                        .last("limit 1").one();
//                //设置订单id
//                order.setId(idWorker.orderNo("B"));
//                order.setGameServer(content.getGameServer());
//                order.setPhone(content.getContact());
//                order.setPaymentMethod(3);//商城
//                order.setTotalAmount(content.getQuantity());
//                order.setStatus(Constants.ZERO);
//                order.setPaymentStatus(Constants.ONE);
//                order.setUserId("user-0");
//                order.setCreateTime(new Date());
//                order.setType(jade.getType());
//                order.setSaleId(jade.getSaleUser());
//                order.setPayAccount(null);
//                order.setNotes("用户出售，用户id:"+SessionContextHolder.getId());
//                //新增订单数据
//                OrderItem orderItem = new OrderItem();
//                orderItem.setId(idWorker.nextId());
//                orderItem.setProductId(jade.getId());
//                orderItem.setQuantityOrder(content.getQuantity());
//                orderItem.setPrice(content.getPrice());
//                orderItem.setOrderId(order.getId());
//                orderItemMapper.insertItem(orderItem);
//                String attrs = jade.getAttrs();
//                JSONObject attrObj = JSONObject.parseObject(attrs);
//                StringBuilder sb = new StringBuilder();
//                sb.append(jade.getLevel());
//                sb.append(" : ");
//                sb.append(orderItem.getPrice());
//                sb.append("(件/元)");
//                if (StrUtil.isNotBlank(attrObj.getString("attr1Val"))){
//                    sb.append(attrObj.getString("attr1"));
//                    sb.append(" : ");
//                    sb.append(attrObj.getString("attr1Val"));
//                }
//                if (StrUtil.isNotBlank(attrObj.getString("attr2Val"))){
//                    sb.append(attrObj.getString("attr2"));
//                    sb.append(" : ");
//                    sb.append(attrObj.getString("attr2Val"));
//                }
//                if (StrUtil.isNotBlank(attrObj.getString("attr3Val"))){
//                    sb.append(attrObj.getString("attr3"));
//                    sb.append(" : ");
//                    sb.append(attrObj.getString("attr3Val"));
//                }
//                if (StrUtil.isNotBlank(attrObj.getString("attr4Val"))){
//                    sb.append(attrObj.getString("attr4"));
//                    sb.append(" : ");
//                    sb.append(attrObj.getString("attr4Val"));
//                }
//                order.setRemark(sb.toString());
//                order.setRate(orderItem.getPrice());
//                orderMapper.insertOrder(order);
//                return order.getId().toString();
//            }
        }else{
            jade.setId(content.getId());
            jade.setCreateTime(new Date());
            jadeMapper.updateById(jade);
        }


        return null;
    }
}
